<template>
  <transition name="list-fade">
    <div class="playlist" v-show="showFlag" @click.stop="hide">
      <div class="list-wrapper" @click.stop>
        <div class="list-header">
          <h1 class="title">
            <i class="icon" :class="modeIcon" @click="changePlayMode"></i>
            <span class="text">{{playModeText}}</span>
            <span class="clear" @click.stop="confirmClearSongs"><i class="icon-clear"></i></span>
          </h1>
        </div>
        <scroll :delayTime="needDelayTime" :data="sequenceList" ref="listContent" class="list-content">
          <transition-group tag="ul" name="slide-fade">
            <li :key="item.mid" class="item" v-for="(item, index) in sequenceList" @click="selectItem(item, index)" ref="liItems">
              <i class="current" :class="getCurrentIcon(item)"></i>
              <span class="text">{{item.name}}</span>
              <span class="like" @click.stop="toggleFavorite(item)">
                <i :class="getFavoriteIcon(item)" ></i>
              </span>
              <span class="delete" @click.stop="handleDeleteSong(item)">
                <i class="icon-delete"></i>
              </span>
            </li>
          </transition-group>
        </scroll>
        <div class="list-operate">
          <div class="add" @click.stop="showAddSong">
            <i class="icon-add"></i>
            <span class="text">添加歌曲到队列</span>
          </div>
        </div>
        <div class="list-close" @click="hide">
          <span>关闭</span>
        </div>
      </div>
      <confirm ref="confirm" text="删除全部歌曲?" confirmBtnText="删除" @confirm="handleConfirm" @cancel="handleCancel"></confirm>
      <add-song ref="addSong"></add-song>
    </div>
  </transition>
</template>

<script type="text/ecmascript-6">
import { mapGetters, mapMutations, mapActions } from 'vuex'
import Scroll from 'base/scroll/scroll'
import { playMode } from 'common/js/config'
import Confirm from 'base/confirm/confirm'
import * as _ from 'lodash'
import { mixinPlayMode } from 'common/js/mixin'
import addSong from 'components/add-song/add-song'

export default {
  name: 'play-list',
  mixins: [ mixinPlayMode ],
  components: {
    Scroll,
    Confirm,
    addSong
  },
  data() {
    return {
      showFlag: false,
      needDelayTime: 20
    }
  },
  props: {
  },
  methods: {
    showAddSong() {
      this.$refs.addSong.show()
    },
    handleConfirm() {
      this.clearSongs()
      this.$refs.confirm.hide()
      this.hide()
    },
    handleCancel() {
      this.$refs.confirm.hide()
    },
    confirmClearSongs() {
      this.$refs.confirm.show()
    },
    handleDeleteSong(item) {
      this.deleteSong(item)
      if (!this.sequenceList.length) this.hide()
    },
    selectItem(item, index) {
      if (item.id === this.currentSong.id) return
      let ind = index
      if (this.mode === playMode.sequence) {
        ind = index
      } else if (this.mode === playMode.random) {
        ind = _.findIndex(this.playlist, o => {
          return o.id === item.id
        })
      }
      this.setCurrentIndex(ind)
      this.setPlayingState(true)
    },
    getCurrentIcon(item) {
      return item.id === this.currentSong.id ? 'icon-play' : ''
    },
    show() {
      this.showFlag = true
      setTimeout(() => {
        this.$refs.listContent.refresh()
      }, 30)
    },
    hide() {
      this.showFlag = false
    },
    ...mapMutations({
      setCurrentIndex: 'SET_CURRENT_INDEX',
      setPlayingState: 'SET_PLAYING_STATE'
    }),
    ...mapActions({
      deleteSong: 'deleteSong',
      clearSongs: 'clearSongs'
    })
  },
  computed: {
    playModeText() {
      return this.mode === playMode.random ? '随机播放' : this.mode === playMode.sequence ? '顺序播放' : '单曲循环'
    },
    ...mapGetters([
      'sequenceList',
      'currentSong',
      'mode',
      'playlist'
    ])
  },
  watch: {
    // currentSong(newVal, oldVal) {
    //   if (newVal === oldVal) return
    //   if (this.showFlag) return
    //   setTimeout(() => {
    //     let ind = _.findIndex(this.sequenceList, (o) => {
    //       return o.id === newVal.id
    //     })
    //     this.$refs.listContent.scrollToElement(this.$refs.liItems[ind], 1000)
    //   }, 30)
    // }
    showFlag() {
      if (!this.showFlag) return
      setTimeout(() => {
        let ind = _.findIndex(this.sequenceList, (o) => {
          return o.id === this.currentSong.id
        })
        this.$refs.listContent.scrollToElement(this.$refs.liItems[ind], 1000)
      }, this.needDelayTime)
    }
  }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin"
  .playlist
    position: fixed
    width: 100%
    top: 0
    bottom: 0
    z-index: 200
    background-color: $color-background-d
    &.list-fade-enter-active, &.list-fade-leave-active
      transition: opacity 0.3s
      .list-wrapper
        transition: all 0.3s
    &.list-fade-enter, &.list-fade-leave-to
      opacity: 0
      .list-wrapper
        transform: translate3d(100%, 100%, 0)
    .list-wrapper
      position: absolute
      left: 0
      bottom: 0
      width: 100%
      background-color: $color-highlight-background
      .list-header
        position: relative
        padding: 20px 30px 10px 20px
        .title
          display: flex
          align-items: center
          .icon
            margin-right: 10px
            font-size: 30px
            color: $color-theme-d
          .text
            flex: 1
            font-size: $font-size-medium
            color: $color-text-l
          .clear
            extend-click()
            .icon-clear
              font-size: $font-size-medium
              color: $color-text-d
      .list-content
        max-height: 240px
        overflow: hidden
        .item
          display: flex
          align-items: center
          height: 40px
          padding: 0 30px 0 20px
          overflow: hidden
          &.slide-fade-enter-active
            transition: all .5s
          &.slide-fade-leave-active
            transition: all .5s
          &.slide-fade-enter, &.slide-fade-leave-to
            height: 0
            opacity: 0
          .current
            flex: 0 0 20px
            width: 20px
            font-size: $font-size-small
            color: $color-theme-d
          .text
            flex: 1
            no-wrap()
            font-size: $font-size-medium
            color: $color-text-d
          .like
            extend-click()
            margin-right: 15px
            font-size: $font-size-small
            color: $color-theme
            .icon-favorite
              color: $color-sub-theme
          .delete
            extend-click()
            font-size: $font-size-small
            color: $color-theme

      .list-operate
        width: 140px
        margin: 20px auto 30px auto
        .add
          display: flex
          align-items: center
          padding: 8px 16px
          border: 1px solid $color-text-l
          border-radius: 100px
          color: $color-text-l
          .icon-add
            margin-right: 5px
            font-size: $font-size-small-s
          .text
            font-size: $font-size-small
      .list-close
        text-align: center
        line-height: 50px
        background: $color-background
        font-size: $font-size-medium-x
        color: $color-text-l
    /*
    @keyframes listanimi
      0%
        transform: scale(1)
      15%
        transform: scale(1.5)
      100%
        transform: scale(0)
    */

</style>
